package com.ctrl.riveradministrator.mvp.view.activity.contact;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ctrl.riveradministrator.R;
import com.ctrl.riveradministrator.base.BaseActivity;
import com.ctrl.riveradministrator.mvp.model.Contact;
import com.ctrl.riveradministrator.mvp.presenter.ContactPresenter;
import com.ctrl.riveradministrator.mvp.view.weight.ContactBar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ContactActivity extends BaseActivity<ContactPresenter> implements ContactView, ContactBar.ISideBarSelectCallBack,
        AdapterView.OnItemClickListener {

    private Toolbar toolbar;
    private TextView titleTv;
    private ImageView leftMenu;

    private ListView listView;
    private ContactBar contactBar;
    private ContactAdapter contactAdapter;
    private List<ContactClassified> list = new ArrayList<>();

    @Override
    protected void initParams(Bundle parms) {

    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_contact;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void initView(View view) {
        toolbar = $(R.id.toolbar);
        titleTv = $(R.id.title_tv);
        leftMenu = $(R.id.left_iv);

        listView = $(R.id.listView);
        contactBar = $(R.id.side_bar);

        pDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
        pDialog.setTitleText("加载中...");
        pDialog.setCancelable(false);

        /*
         * 设置toolbar
         * */
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setTitle("");
        titleTv.setText(R.string.contact);
        leftMenu.setBackground(getDrawable(R.drawable.ic_chevron_left_24dp));
    }

    @Override
    protected void doBusiness(Context mContext) {
        mPresenter.getContact("0", "20");
    }

    @Override
    protected void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.left_iv:
                finish();
                break;
        }
    }

    @Override
    protected void setListener() {
        leftMenu.setOnClickListener(this);
        contactBar.setOnStrSelectCallBack(this);
        listView.setOnItemClickListener(this);
    }

    @Override
    public void handleMessage(Message message, int what) {

    }

    @Override
    protected ContactPresenter createPresenter() {
        return new ContactPresenter(this);
    }

    @Override
    public void onSuccess(List<Contact> contacts) {

        if (contacts != null && contacts.size()!=0){
            for (int i = 0; i < contacts.size(); i++) {
                list.add(new ContactClassified(contacts.get(i).getUserName(), contacts.get(i).getPhonenumber()));
            }
        }
        Collections.sort(list);
        contactAdapter = new ContactAdapter(this, list);
        listView.setAdapter(contactAdapter);
    }

    @Override
    public void onFailed(String msg) {

    }

    @Override
    public void onSelectStr(int index, String selectStr) {
        for (int i = 0; i < list.size(); i++) {
            if (selectStr.equalsIgnoreCase(list.get(i).getFirstLetter())) {
                listView.setSelection(i); // 选择到首字母出现的位置
                return;
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + list.get(i).getTel()));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    @Override
    public void showLoading() {
        super.showLoading();
        pDialog.show();
    }

    @Override
    public void hideLoading() {
        super.hideLoading();
        pDialog.cancel();
    }
}
